package com.nangle.managehost.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.nangle.managehost.R;
import com.nangle.managehost.dialog.MyDialog;
import com.nangle.managehost.entity.Notice;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YFB on 2017/3/24.
 */

public class NoticeAdapter extends BaseAdapter{
    private List<Notice> noticeList = new ArrayList<>();
    private LayoutInflater mInflater;
    private Activity mContext;
    public NoticeAdapter(Activity context, List<Notice> noticeList){
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.noticeList = noticeList;
    }
    @Override
    public int getCount() {
        return noticeList.size();
    }

    @Override
    public Object getItem(int position) {
        return noticeList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.layout_notice_item, parent,false);
            viewHolder = new ViewHolder();
            viewHolder.tvNoticeContent = (TextView)convertView.findViewById(R.id.notice_content);
            viewHolder.tvNoticeType = (TextView)convertView.findViewById(R.id.notice_type);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Notice notice = noticeList.get(position);
        viewHolder.tvNoticeContent.setText(notice.getContent());
        int type = notice.getType();
        if(type == 0 ){
            viewHolder.tvNoticeType.setText("已发送");
            viewHolder.tvNoticeType.setBackgroundResource(R.drawable.light_gray_bg);
            viewHolder.tvNoticeType.setOnClickListener(null);
        }else{
            viewHolder.tvNoticeType.setText("发送到患者对应床头");
            viewHolder.tvNoticeType.setBackgroundResource(R.drawable.light_blue_bg);
            viewHolder.tvNoticeType.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showFailDialog("发送失败，请检查网络");
                }
            });
        }
        return convertView;
    }
    private final class ViewHolder {
        private TextView tvNoticeContent;
        private TextView tvNoticeType;
    }
    private void showFailDialog(String message){
        View view = mContext.getLayoutInflater().inflate(R.layout.add_bed_fail_dialog, null);
        TextView tvContent = (TextView)view.findViewById(R.id.tv_dialog_content);
        tvContent.setText(message);
        final MyDialog builder = new MyDialog(mContext,0,0,view,R.style.DialogTheme);
        builder.setCancelable(true);
        builder.show();
    }
}
